package com.mes.warehouse.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.pojo.warehouse.WarehouseArea;
import com.mes.pojo.warehouse.WarehouseLocation;
import com.mes.warehouse.mapper.WarehouseAreaMapper;
import com.mes.warehouse.mapper.WarehouseLocationMapper;
import com.mes.warehouse.server.WarehouseLocationServer;
import com.mes.warehouse.server.WarehouseServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/25 16:55
 */
@Service
public class WarehouseLocationImpl implements WarehouseLocationServer {
    @Autowired
    private WarehouseLocationMapper warehouseLocationMapper;
    @Autowired
    private WarehouseAreaMapper warehouseAreaMapper;

    @Override
    public MESResult queryAll(WarehouseLocation warehouseLocation, Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum, limit);
        List<WarehouseLocation> warehouseLocations = warehouseLocationMapper.selectAll(warehouseLocation);
        PageInfo<WarehouseLocation> warehouseLocationPageInfo = new PageInfo<>(warehouseLocations);
        return MESResult.success(warehouseLocationPageInfo);
    }

    @Override
    public MESResult queryById(String warehouseLocationId, String warehouseAreaId) {
        return MESResult.success(warehouseLocationMapper.selectById(warehouseLocationId, warehouseAreaId));
    }

    @Override
    public MESResult deleteWarehouseLocation(String warehouseLocationId, String warehouseAreaId) {
        WarehouseLocation warehouseLocation = warehouseLocationMapper.selectById(warehouseLocationId, warehouseAreaId);
        warehouseLocation.setWarehouseLocationStatus(99);
        return MESResult.success(warehouseLocationMapper.updateWarehouseLocation(warehouseLocation));
    }

    @Override
    public MESResult updateWarehouseLocation(WarehouseLocation warehouseLocation) {
        WarehouseLocation same = warehouseLocationMapper.selectByName(warehouseLocation.getWarehouseLocationName(), warehouseLocation.getWarehouseAreaId());
        if (same != null && !same.getWarehouseLocationId().equals(warehouseLocation.getWarehouseLocationId())) {
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        Long areaInUse = warehouseAreaMapper.countTotalArea(warehouseLocation.getWarehouseAreaId());
        if (areaInUse == null) {
            areaInUse = 0L;
        }
        if ((warehouseLocation.getWarehouseLocationArea()
                + areaInUse
                //减去原本的面积占地
                - same.getWarehouseLocationArea()
        )
                > warehouseAreaMapper.selectByIdNoF(warehouseLocation.getWarehouseAreaId()).getWarehouseAreaArea()) {
            return MESResult.error(Constant.RESPONSE_STATUS_ERROR);
        }
        WarehouseLocation spaceInUse = warehouseLocationMapper.judgeWhetherInUse(warehouseLocation.getWarehouseAreaId(), warehouseLocation.getWarehouseLocationNumberX(), warehouseLocation.getWarehouseLocationNumberY(), warehouseLocation.getWarehouseLocationNumberZ());
        WarehouseArea warehouseArea = warehouseAreaMapper.selectByIdNoF(warehouseLocation.getWarehouseAreaId());
        //确保当前库位并未使用，并且不会超越库区限制
        if ((spaceInUse != null || warehouseLocation.getWarehouseLocationNumberX() > warehouseArea.getWarehouseAreaNumberX()
                || warehouseLocation.getWarehouseLocationNumberY() > warehouseArea.getWarehouseAreaNumberY()
                || warehouseLocation.getWarehouseLocationNumberZ() > warehouseArea.getWarehouseAreaNumberZ()) &&
                !spaceInUse.getWarehouseLocationId().equals(warehouseLocation.getWarehouseLocationId())
        ) {
            return MESResult.error(Constant.RESPONSE_STATUS_OVER_SIZE);
        }
        return MESResult.success(warehouseLocationMapper.updateWarehouseLocation(warehouseLocation));
    }

    @Override
    public MESResult insertWarehouseLocation(WarehouseLocation warehouseLocation) {
        WarehouseLocation same = warehouseLocationMapper.selectByName(warehouseLocation.getWarehouseLocationName(), warehouseLocation.getWarehouseAreaId());
        if (same != null) {
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        Long areaInUse = warehouseAreaMapper.countTotalArea(warehouseLocation.getWarehouseAreaId());
        if (areaInUse == null) {
            areaInUse = 0L;
        }
        if ((warehouseLocation.getWarehouseLocationArea()
                + areaInUse
        )
                > warehouseAreaMapper.selectByIdNoF(warehouseLocation.getWarehouseAreaId()).getWarehouseAreaArea()) {
            return MESResult.error(Constant.RESPONSE_STATUS_ERROR);
        }
        WarehouseLocation spaceInUse = warehouseLocationMapper.judgeWhetherInUse(warehouseLocation.getWarehouseAreaId(), warehouseLocation.getWarehouseLocationNumberX(), warehouseLocation.getWarehouseLocationNumberY(), warehouseLocation.getWarehouseLocationNumberZ());
        WarehouseArea warehouseArea = warehouseAreaMapper.selectByIdNoF(warehouseLocation.getWarehouseAreaId());
        //确保当前库位并未使用，并且不会超越库区限制
        if (spaceInUse != null || warehouseLocation.getWarehouseLocationNumberX() > warehouseArea.getWarehouseAreaNumberX()
                || warehouseLocation.getWarehouseLocationNumberY() > warehouseArea.getWarehouseAreaNumberY()
                || warehouseLocation.getWarehouseLocationNumberZ() > warehouseArea.getWarehouseAreaNumberZ()
        ) {
            return MESResult.error(Constant.RESPONSE_STATUS_OVER_SIZE);
        }
        String maxId = warehouseLocationMapper.selectMxId();
        String[] split = maxId.split("_");
        int num = Integer.parseInt(split[1]);
        num = num + 1;
        String strNum = null;
        if (num > 0 && num < 10) {
            strNum = "000" + num;
        } else if (num < 100) {
            strNum = "00" + num;
        } else if (num < 1000) {
            strNum = "0" + num;
        } else {
            strNum = num + "";
        }
        warehouseLocation.setWarehouseLocationId(split[0] + "_" + strNum);
        return MESResult.success(warehouseLocationMapper.insertWarehouseLocation(warehouseLocation));
    }
}
